#include "Drv\H\Function_Init.H"
#include "Api_Tick.h"

#define WDT_Feed(); WDTCON |= 0x10;		//清看门狗

//各systick频率下的tick时长
#define SYSTICK_CYCLE_1000MS ((unsigned long int)1000000)
#define SYSTICK_CYCLE_100MS ((unsigned long int)100000)
#define SYSTICK_CYCLE_10MS ((unsigned long int)10000)
#define SYSTICK_CYCLE_5MS ((unsigned long int)5000)
#define SYSTICK_CYCLE_1MS ((unsigned long int)1000)
#define SYSTICK_CYCLE_500US ((unsigned long int)500)
#define SYSTICK_CYCLE_250US ((unsigned long int)250)
#define SYSTUCK_CYCLE_125US ((unsigned long int)125)
#define SYSTICK_CYCLE_50US ((unsigned long int)50)
#define SYSTICK_CYCLE_10US ((unsigned long int)10)
#define SYSTICK_CYCLE_1US ((unsigned long int)1)
    

#define SYSTICK_CYCLE_TIME SYSTICK_CYCLE_1MS 
#define SYSTICK_PER_MS ((float)SYSTICK_CYCLE_1MS / (float)SYSTICK_CYCLE_TIME)
//#define SYSTICK_PER_US ((float)SYSTICK_CYCLE_1US / (float)SYSTICK_CYCLE_TIME)
//#define SYSTICK_LOAD (SystemCoreClock /(unsigned long int)(SYSTICK_CYCLE_1000MS/SYSTICK_CYCLE_TIME))
        
        
static TIMER_CB timerCB;

void SysTick_Handler(void)
{
    static unsigned char tick=0;
    WDT_Feed();
    
    if(++tick==SYSTICK_PER_MS)
    {
        tick=0;
        Api_Tick_Ms();
    }
}


void Api_Tick_Ms(void)
{
	timerCB.msTick++;
	
	if(timerCB.msTick%10==0)
	{
		timerCB.flag_10ms++;
		if(timerCB.msTick%50==0)
		{
			timerCB.flag_50ms++;
			if(timerCB.msTick%100==0)
			{
				timerCB.flag_100ms++;
				if(timerCB.msTick%500==0)
				{
					timerCB.flag_500ms++;
					if(timerCB.msTick%1000==0)
					{
						timerCB.flag_1000ms++;
						timerCB.msTick=0;
					}
				}
			}
		}
	}
}
void Api_Tick_Init(void)
{
    
	timerCB.msTick=0;
	timerCB.flag_1000ms=0;
	timerCB.flag_500ms=0;
	timerCB.flag_100ms=0;
	timerCB.flag_50ms=0;
	timerCB.flag_10ms=0;
}

unsigned short int Api_Tick_GetmsTick(void)
{
	return timerCB.msTick;
}

unsigned char Api_Tick_PopFlag_10ms(void)
{
    if(timerCB.flag_10ms==0)
        return 0;
	return timerCB.flag_10ms--;
}
unsigned char Api_Tick_PopFlag_50ms(void)
{
    if(timerCB.flag_50ms==0)
        return 0;
	return timerCB.flag_50ms--;
}
unsigned char Api_Tick_PopFlag_100ms(void){
    if(timerCB.flag_100ms==0)
        return 0;
	return timerCB.flag_100ms--;
}

unsigned char Api_Tick_PopFlag_500ms(void){
    if(timerCB.flag_500ms==0)
        return 0;
	return timerCB.flag_500ms--;
}
unsigned char Api_Tick_PopFlag_1000ms(void){
    if(timerCB.flag_1000ms==0)
        return 0;
	return timerCB.flag_1000ms--;
}

//void Api_Tick_BlockDelayUs(unsigned char us)
//{
//    unsigned long int start, now, delta, reload, us_tick;
//    start = SysTick->VAL;
//    reload = SysTick->LOAD;
//    us_tick = SystemCoreClock / 1000000UL;
//    do {
//        now = SysTick->VAL;
//        delta = start > now ? start - now : reload + start - now;
//    } while(delta < us_tick * us);
//}

//void Api_Tick_BlockDelayMs(unsigned char ms)
//{
//    unsigned short int us100=ms*10;
//    for(uint16_t i =0;i<us100;i++)
//    {
//        Api_Tick_BlockDelayUs(100);
//    }
//}
